<template>
	<view class="mainpadding2" style="padding-bottom: 150rpx;">
		<u-navbar :title="listid?'編輯區域':'添加區域'" placeholder autoBack>
			<view class="flexright xiaohui" slot="right" @click="jumpurl('/pages/workbench/jiejiarigl?type=1')">導入節假日</view>
		</u-navbar>
		<view class="mainpadding_top ffffff radius flexbetween">
			<view class="main_size bold hei_text">名稱</view>
			<input type="text" class="textright" placeholder="請输入區域名稱" v-model="name">
		</view>
		<view class="mainpadding_top ffffff radius flexbetween">
			<view class="main_size bold hei_text">排序</view>
			<input type="number" class="textright" placeholder="請输入排序" v-model="weigh">
		</view>
		<view class="mainpadding_top ffffff radius" v-for="(item,index) in jjrprice_data" :key="index">
			<view class="flexbetween">
				<view class="main_size bold hei_text">{{item.name}}</view>
				<view class="flexleft margin_left" @click.stop="delitem(index)">
					<u-icon name="trash" color="#999" size="16"></u-icon>
					<view class="xiaohei">刪除</view>
				</view>
			</view>
			<view class="flexleft flex-wrap">
				<view class="xiaohui margin_top2 margin_right1 riqik" v-for="ite in item.date_time_text" :key="ite">{{ite}}</view>
			</view>
			<view class="flexbetween margin_top2">
				<view class="main_size bold hei_text">車泊</view>
				<input type="text" placeholder="請輸入車泊價格" v-model="item.cb_price" class="textright">
			</view>
			<view class="flexbetween margin_top2">
				<view class="main_size bold hei_text">搭帳</view>
				<input type="text" placeholder="請輸入搭帳價格" v-model="item.dz_price" class="textright">
			</view>
			<view class="flexbetween margin_top2">
				<view class="main_size bold hei_text">夜衝</view>
				<input type="text" placeholder="請輸入夜衝價格" v-model="item.yc_price" class="textright">
			</view>
			<view class="flexbetween margin_top2">
				<view class="main_size bold hei_text">休息</view>
				<input type="text" placeholder="請輸入休息價格" v-model="item.xx_price" class="textright">
			</view>
			<view class="flexbetween margin_top2">
				<view class="main_size bold hei_text">住宿</view>
				<input type="text" placeholder="請輸入住宿價格" v-model="item.zs_price" class="textright">
			</view>
		</view>
		
		<view class="posibottom mainpadding2">
			<view class="submitbtn" @click="submit">確定</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				listid: "",
				jjrprice_data:[],
				weigh: "",
				name: "",
				issubmit:false
			}
		},
		onLoad(options) {
			if (options.id) {
				this.listid = options.id
				uni.setNavigationBarTitle({
					title:"編輯區域"
				})
				this.getDetail()
			}
			uni.$on("checkjjrjg",data =>{
				this.jjrprice_data  = data
			})
		},
		methods: {
			delitem(index){
				this.jjrprice_data.splice(index,1)
			},
			getDetail(){
				this.http.request("/api/shop/shopAreaDetail", 'GET', {
					id:this.listid
				}).then(res=>{
					let data = res.data
					this.name=data.name
					this.weigh=data.weigh
					let arr = []
					data.shopjjr.forEach(item=>{
						arr.push({
							shoptable_jjrprice_id:item.id,
							name:item.jjrprice.name,
							date_time_text:item.jjrprice.date_time_text,
							cb_price:item.cb_price,
							dz_price:item.dz_price,
							yc_price:item.yc_price,
							xx_price:item.xx_price,
							zs_price:item.zs_price
						})
					})
					this.jjrprice_data = arr
				})
			},
			submit() {
				if (this.name == "") {
					this.http.toast("請輸入區域名稱")
					return false
				}
				if (this.weigh == "") {
					this.http.toast("請輸入排序")
					return false
				}
				if(this.issubmit) return
				this.issubmit = true
				let jjrprice_data = JSON.parse(JSON.stringify(this.jjrprice_data))
				jjrprice_data.forEach(item=>{
					delete item.name
					delete item.date_time_text
				})
				let url = "/api/shop/addShopArea"
				let data = {
					shop_id: uni.getStorageSync("shop_id"),
					name: this.name,
					weigh: this.weigh,
					jjrprice_data
				}
				if(this.listid){
					url = "/api/shop/editShopArea"
					this.$set(data,"id",this.listid)
				}
				this.http.request(url, 'POST', data).then(res => {
					if (res.code == 1) {
						this.http.toast(res.msg)
						setTimeout(()=>{
							uni.navigateBack(1)
						},500)
					}else{
						this.issubmit = false
						this.http.toast(res.msg)
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.riqik{
		border: 1rpx solid #999;
		padding: 5rpx 10rpx;
	}
</style>